package cn.iocoder.yudao.module.acm.service.onboardsupply;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.collection.MapUtils;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.acm.controller.admin.onboardsupply.vo.OnboardSupplyPageReqVO;
import cn.iocoder.yudao.module.acm.controller.admin.onboardsupply.vo.OnboardSupplyRespVO;
import cn.iocoder.yudao.module.acm.controller.admin.onboardsupply.vo.OnboardSupplySaveReqVO;
import cn.iocoder.yudao.module.acm.dal.dataobject.onboardsupply.OnboardSupplyDO;
import cn.iocoder.yudao.module.acm.dal.mysql.onboardsupply.OnboardSupplyMapper;
import cn.iocoder.yudao.module.acm.enums.ACMSupplyCategoryEnum;
import cn.iocoder.yudao.module.acm.enums.ACMSupplyStandardEnum;
import cn.iocoder.yudao.module.erp.controller.admin.product.vo.product.ErpProductRespVO;
import cn.iocoder.yudao.module.erp.dal.dataobject.product.ErpProductCategoryDO;
import cn.iocoder.yudao.module.erp.dal.dataobject.product.ErpProductDO;
import cn.iocoder.yudao.module.erp.dal.dataobject.product.ErpProductUnitDO;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Map;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertMap;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertSet;
import static cn.iocoder.yudao.module.acm.enums.ErrorCodeConstants.ONBOARD_SUPPLY_NOT_EXISTS;
import static cn.iocoder.yudao.module.erp.enums.ErrorCodeConstants.PRODUCT_NOT_ENABLE;
import static cn.iocoder.yudao.module.erp.enums.ErrorCodeConstants.PRODUCT_NOT_EXISTS;

/**
 * 航机管理-机供品分类管理 Service 实现类
 *
 * @author yisla
 */
@Service
@Validated
public class OnboardSupplyServiceImpl implements OnboardSupplyService {

    @Resource
    private OnboardSupplyMapper onboardSupplyMapper;

    @Override
    public Long createOnboardSupply(OnboardSupplySaveReqVO createReqVO) {
        // 插入
        OnboardSupplyDO onboardSupply = BeanUtils.toBean(createReqVO, OnboardSupplyDO.class);
        onboardSupplyMapper.insert(onboardSupply);
        // 返回
        return onboardSupply.getId();
    }

    @Override
    public void updateOnboardSupply(OnboardSupplySaveReqVO updateReqVO) {
        // 校验存在
        validateOnboardSupplyExists(updateReqVO.getId());
        // 更新
        OnboardSupplyDO updateObj = BeanUtils.toBean(updateReqVO, OnboardSupplyDO.class);
        onboardSupplyMapper.updateById(updateObj);
    }

    @Override
    public void deleteOnboardSupply(Long id) {
        // 校验存在
        validateOnboardSupplyExists(id);
        // 删除
        onboardSupplyMapper.deleteById(id);
    }

    @Override
        public void deleteOnboardSupplyListByIds(List<Long> ids) {
        // 校验存在
        validateOnboardSupplyExists(ids);
        // 删除
        onboardSupplyMapper.deleteByIds(ids);
        }

    private void validateOnboardSupplyExists(List<Long> ids) {
        List<OnboardSupplyDO> list = onboardSupplyMapper.selectByIds(ids);
        if (CollUtil.isEmpty(list) || list.size() != ids.size()) {
            throw exception(ONBOARD_SUPPLY_NOT_EXISTS);
        }
    }

    private void validateOnboardSupplyExists(Long id) {
        if (onboardSupplyMapper.selectById(id) == null) {
            throw exception(ONBOARD_SUPPLY_NOT_EXISTS);
        }
    }

    @Override
    public OnboardSupplyDO getOnboardSupply(Long id) {
        return onboardSupplyMapper.selectById(id);
    }

    @Override
    public PageResult<OnboardSupplyRespVO> getOnboardSupplyPage(OnboardSupplyPageReqVO pageReqVO) {
        PageResult<OnboardSupplyDO> doPageResults = onboardSupplyMapper.selectPage(pageReqVO);
        return new PageResult<>(buildList(doPageResults.getList()), doPageResults.getTotal());
    }

    private List<OnboardSupplyRespVO> buildList(List<OnboardSupplyDO> list) {
        if (CollUtil.isEmpty(list)) {
            return Collections.emptyList();
        }
        // 1. 转换成 VO
        return BeanUtils.toBean(list, OnboardSupplyRespVO.class, clueVO -> {
            if (StrUtil.isNotEmpty(clueVO.getCategoryType()))
                clueVO.setCategoryName(ACMSupplyCategoryEnum.getDesc(clueVO.getCategoryType()));
            if (StrUtil.isNotEmpty(clueVO.getStandard()))
                clueVO.setStandardName(ACMSupplyStandardEnum.getDesc(clueVO.getStandard()));
        });
    }

    @Override
    public List<OnboardSupplyRespVO> getProductVOList(Collection<Long> ids) {
        if (CollUtil.isEmpty(ids)) {
            return Collections.emptyList();
        }
        List<OnboardSupplyDO> list = onboardSupplyMapper.selectBatchIds(ids);
        return buildList(list);
    }

    @Override
    public List<OnboardSupplyDO> validProductList(Collection<Long> ids) {
        if (CollUtil.isEmpty(ids)) {
            return Collections.emptyList();
        }
        List<OnboardSupplyDO> list = onboardSupplyMapper.selectBatchIds(ids);
        Map<Long, OnboardSupplyDO> productMap = convertMap(list, OnboardSupplyDO::getId);
        for (Long id : ids) {
            if (productMap.get(id) == null) {
                throw exception(PRODUCT_NOT_EXISTS);
            }
        }
        return list;
    }


}
